package zyx20230706;



import javax.swing.*;
import java.awt.*;

public class DrawUI {
    public void initUI() {
        //显示界面
        JFrame jf = new JFrame();//JFrame  默认是边框布局
        jf.setSize(900, 900);
        jf.setTitle("画图板");
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);

        //设置窗体背景色
        jf.getContentPane().setBackground(Color.WHITE);

        //边框布局
        jf.setLayout(new BorderLayout());

        DrawMouse mouse = new DrawMouse(); //创建监听器对象

        //按钮面板  对象JPanel  默认是流式布局
        JPanel northPanel = new JPanel();
        northPanel.setBackground(Color.WHITE);
        northPanel.setPreferredSize(new Dimension(0,40));
        jf.add(northPanel,BorderLayout.NORTH);

        //图形按钮设定
        String[] typeStrs = {"直线","三角形","矩形","铅笔","圆","多边形","随机数"};
        for(int i = 0; i < typeStrs.length; i++){
            JButton jbu = new JButton (typeStrs[i]);
            northPanel.add (jbu);
            jbu.addActionListener (mouse);// 给按钮添加监听器
        }

        //颜色按钮设定
        Color[] color={Color.blue,Color.GREEN,Color.RED,new Color(200,100,200)};
        for(int i=1;i< color.length;i++){
            JButton btu=new JButton();
            btu.setBackground(color[i]);
            btu.setPreferredSize(new Dimension(30,30));
            northPanel.add(btu);
            btu.addActionListener(mouse);
        }

        //图画面板
        MFrame drawPanel = new MFrame();
        drawPanel.setBackground(Color.WHITE);
        jf.add(drawPanel,BorderLayout.CENTER);

        jf.setVisible(true);//画笔必须在可见之后

        //可视化之后给窗体添加鼠标监听器()
        drawPanel.addMouseListener(mouse);
        drawPanel.addMouseMotionListener(mouse);

        //获取Graphics画笔
        Graphics g = drawPanel.getGraphics();

        //把画笔对象传递给DrawMouse类
        mouse.gr = g;

        //把DrawMouse类的中shapeArr数组传递给MFrame类
        drawPanel.shapeArr = mouse.shapeArr;

    }

    public static void main(String[] args) {
        DrawUI ui = new DrawUI();
        ui.initUI();
        //54-55的代码优化：new DrawUI ().initUI ();
    }
}

